package com.inkrl.order.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inkrl.order.mapper.OrderMapper;
import com.jt.common.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired(required = false)
    private OrderMapper orderMapper;
    private StringRedisTemplate template;
    private static final ObjectMapper mapper = new ObjectMapper();
    @Override
    public List<Map<String, Object>> queryOrder(String token) {
        ValueOperations<String,String> opsValue = template.opsForValue();
        if (template.hasKey(token)){
            String userJson = opsValue.get(token);
            try {
                User user = mapper.readValue(userJson,User.class);
                List<Map<String, Object>> list = orderMapper.selectByOrderIdJoinBook(user.getPhone());
                return list;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> queryDetails(String token) {
        ValueOperations<String,String> opsValue = template.opsForValue();
        if (template.hasKey(token)){
            String userJson = opsValue.get(token);
            try {
                User user = mapper.readValue(userJson,User.class);
                List<Map<String, Object>> list = orderMapper.selectByOrderIdJoinBook1(user.getPhone());
                return list;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    static SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmm");
    @Override
    public Map<String, Object> addOrder(String token,String id) {
        ValueOperations<String,String> opsValue = template.opsForValue();
        if (template.hasKey(token)){
            String userJson = opsValue.get(token);
            try {
                User user = mapper.readValue(userJson,User.class);
                Date date = new Date();
                String orderId = user.getPhone().substring(8)+s.format(date);
                Order order = new Order();
                order.setOrderId(orderId);
                order.setUserId(user.getPhone());
                order.setSta("待处理");
                order.setPhone(user.getPhone());
                orderMapper.insertOrder(order);
                String[] ids =  id.split(",");
                Map<String,Object> cartItem = null;
                OrderItem orderItem = null;
                for(String item:ids){
                    cartItem = orderMapper.selectByRidBook(item);
                    orderItem = new OrderItem();
                    orderItem.setCount((Integer) cartItem.get("count"));
                    orderItem.setOrderId(orderId);
                    orderItem.setPrice((Double) cartItem.get("price"));
                    orderItem.setProduct((String) cartItem.get("product"));
                    orderMapper.insertOrderItem(orderItem);
                }
                List<Map<String,Object>> addresses = orderMapper.selectAddress();
                List<Map<String,Object>> oItems = orderMapper.selectByOrderIdBook(orderId);
                Map<String,Object> map = new HashMap<>();
                map.put("addresses",addresses);
                map.put("oItems",oItems);
                map.put("order",order);
                return map;

            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
